package com.jinxin.phonecloudmusic.myapplication.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jinxin.phonecloudmusic.myapplication.R;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.BillboardList;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.Play;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.SongList;
import com.jinxin.phonecloudmusic.myapplication.event.MusicControlEvent;
import com.jinxin.phonecloudmusic.myapplication.model.MainPageModel;
import com.jinxin.phonecloudmusic.myapplication.utils.RxBus;
import com.squareup.picasso.Picasso;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ZJ on 2017/3/7 0007.
 * 榜单适配器
 */
public class MusicListAdapter extends QuickAdapter<SongList>{
    private MainPageModel mainPageModel=new MainPageModel();
    public MusicListAdapter(Context context, int res, List<SongList> data) {
        super(context, res, data);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public View getItemView(int position, View convertView, ViewHolder holder) {
        final SongList songList=data.get(position);
        TextView tvListNum=holder.getView(R.id.tv_home_page_list_num);
        TextView tvListName=holder.getView(R.id.tv_home_page_list_name);
        TextView tvSongerName=holder.getView(R.id.tv_home_page_songer_name);
        ImageView ivMusicDownLoad=holder.getView(R.id.iv_music_download);
        try {
            if (Integer.parseInt(songList.getRank())<10){
                tvListNum.setText("0"+songList.getRank());
            }else {
                tvListNum.setText(songList.getRank());
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        tvListName.setText(songList.getTitle());
        tvSongerName.setText(songList.getAuthor());
        //下载当前歌曲
        if (null!=ivMusicDownLoad)
        ivMusicDownLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用播放的接口获取play
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Play play=mainPageModel.playMusic("play",songList.getSong_id());
                        RxBus.getDefault().post(new MusicControlEvent(6,play));
                    }
                }).start();
            }
        });
        return convertView;
    }


}
